import 'package:flutter/material.dart';
import 'package:flutter_app/LoginActivity.dart';

class SplashActivity extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => SplashActivityState();
}

class SplashActivityState extends State<SplashActivity>
    with SingleTickerProviderStateMixin {
  int _time = 5;
  AnimationController _controller;
  Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(milliseconds: 1000),
      vsync: this,
    )..forward();

    _animation = CurvedAnimation(
        parent: _controller,
        curve: Interval(0.0, 1.0, curve: Curves.fastOutSlowIn))
      ..addStatusListener((AnimationStatus status) {
        print('$status');
        if (status == AnimationStatus.completed) {
          if (_time > 1) {
            setState(() {
              _time--;
            });
            _controller.reset();
            _controller.forward();
          } else {
            Navigator.of(context).pushReplacement(
                MaterialPageRoute<void>(builder: (BuildContext context) {
              return LoginActivity();
            }));
          }
        }
      });
  }

  @override
  void dispose() {
    _controller.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
//        appBar:  AppBar(
//          title:  Text('Welcome to Flutter'),
//        ),
        body: Stack(
          children: <Widget>[
            Image.network(
              'http://r1.ykimg.com/material/0A03/A1/201803/0321/3001589/1521618754561/0D0100005AB217061741791826303252.jpg',
              fit: BoxFit.fill,
            ),
            Column(
              children: <Widget>[
                Container(
                  alignment: Alignment.topRight,
                  padding: EdgeInsets.only(right: 25.0, top: 25.0),
                  child: SizedBox(
                    height: 40.0,
                    width: 40.0,
                    child: Stack(
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.only(left: 2.0, top: 2.0),
                          child: AnimatedBuilder(
                            animation: _animation,
                            builder: (BuildContext context, Widget child) {
                              return CircularProgressIndicator(
                                value: _animation.value,
                              );
                            },
                          ),
                        ),
                        Center(
                          child: Text(
                            '$_time',
                            style: TextStyle(
                              fontSize: 22.0,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: Center(),
                ),
                Padding(
                  padding: EdgeInsets.only(bottom: 40.0),
                  child: Row(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(left: 20.0),
                        child: Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_xiami.png',
                              height: 40.0,
                              width: 40.0,
                            ),
                            Text(
                              '    虾米音乐\n    听见不同',
                              style: TextStyle(fontSize: 18.0),
                            ),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Text(''),
                      ),
                      Padding(
                        padding: EdgeInsets.only(right: 20.0),
                        child: ButtonTheme.fromButtonThemeData(
                          data: ButtonTheme.of(context).copyWith(
                                shape: StadiumBorder(),
                              ),
                          child: OutlineButton(
                            child: Text('SKIP'),
                            onPressed: () {
                              Navigator.of(context).pushReplacement(
                                  MaterialPageRoute<void>(
                                      builder: (BuildContext context) {
                                return LoginActivity();
                              }));
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
